package com.erhuogo.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.CityDao;
import com.erhuogo.dao.SchoolDao;
import com.erhuogo.entity.City;
import com.erhuogo.entity.School;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.CityService;
import com.erhuogo.util.DateUtils;

/**
 * 城市Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:55:59
 */
public class CityServiceImpl implements CityService {
	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	// 日志
	private static Logger logger = LoggerFactory.getLogger(CityServiceImpl.class);
	
	// 注入CityDao
	private CityDao cityDao;

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}
	
	// 注入SchoolDao
	private SchoolDao schoolDao;

	public SchoolDao getSchoolDao() {
		return schoolDao;
	}

	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}

	// 1.根据省份Id获取所有的城市Service实现类
	@Override
	public List<City> findCitysByProId(Integer provinceId) {
		List<City> listCitys = null;
		if (provinceId != null){
		    listCitys = cityDao.findCitysByProId(provinceId);
		}
		return listCitys;
	}

	// 2.分页条件搜索城市信息的Service实现类
	@Override
	public GridPageBean<City> findByPage(String pageNo, String pageSize, String id, String name, String startDate,
			String endDate, String provinceId) {
		Integer pNo = 1;// 默认页码
		Integer pSize = 10;// 默认记录数
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = cityDao.countByParams(id, name, startDate, endDate, provinceId);
		GridPageBean<City> result = new GridPageBean<City>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<City> rows = cityDao.findPageByParams(id,name,startDate,endDate,provinceId,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<City> rows = cityDao.findPageByParams(id,name,startDate,endDate,provinceId,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 3.根据id获取城市信息Service实现类
	@Override
	public City findById(String idStr) {
		City city = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			city = cityDao.findCitysById(id);
		}
		return city;
	}

	// 4.更新或者保存城市信息Service实现类
	@Override
	public void save(String idStr, String name, String provinceIdStr) throws Exception {
		City city = new City();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			city = cityDao.findCitysById(id);
		}else{
			// id为空则是添加新的三级分类
			city.setCreateTime(DateUtils.DateToStr(new Date()));
			city.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(name)){
			city.setName(name);
		}
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			city.setProvinceId(provinceId);
		}
		city.setAlterTime(DateUtils.DateToStr(new Date()));
		cityDao.saveCity(city);
	}

	// 5.更新城市的启用状态Servic实现类
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			cityDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
			updateAllSchoolStatusByTwoId(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			cityDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
			updateAllSchoolStatusByTwoId(id, OpenStatus.SHIELD.getStatusCode());
		}
	}
	
	/**
	 * 6.更新城市下所有高校的状态
	 * @param id
	 * @throws Exception
	 */
	private void updateAllSchoolStatusByTwoId(Integer id, Integer status) throws Exception {
		List<School> listSchool = cityDao.findAllSchoolByCityId(id);
		if(listSchool!=null && listSchool.size()>0){
			logger.info("该城市下查询到["+listSchool.size()+"]个高校,正在同步"+getTransStatus(status));
			for (School school : listSchool) {
				school.setStatus(status);
				schoolDao.saveSchool(school);
			}
		}else{
			logger.info("该城市下未查询到高校");
		}
	}
	
	/**
	 * 7.获取正在如何更新状态
	 * @param status
	 * @return
	 */
	public String getTransStatus(Integer status){
		String result = null;
		if(status!=null){
			if(status==0){
				result = "启用";
			}else{
				result = "屏蔽";
			}
		}
		return result;
	}

	
}
